﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2008 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2008-8-13 17:43:17
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL
{
    /// <summary>
    /// 数据访问类 USER_FUNCOPERATE
    /// </summary>
    public class USER_FUNCOPERATE : IUSER_FUNCOPERATE
    {
        public USER_FUNCOPERATE()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.USER_FUNCOPERATE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [USER_FUNCOPERATE](");
            strSql.Append("[UFO_UGCODE],[UFO_FUNCCODE],[UFO_OPERATECODE])");
            strSql.Append(" values (");
            strSql.Append("@UFO_UGCODE,@UFO_FUNCCODE,@UFO_OPERATECODE)");
            SqlParameter[] parameters = {
                    new SqlParameter("@UFO_UGCODE", SqlDbType.VarChar,20),
                    new SqlParameter("@UFO_FUNCCODE", SqlDbType.VarChar,15),
                    new SqlParameter("@UFO_OPERATECODE", SqlDbType.VarChar,15)
            };
            parameters[0].Value = model.UFO_UGCODE;
            parameters[1].Value = model.UFO_FUNCCODE;
            parameters[2].Value = model.UFO_OPERATECODE;

            if (SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0)
                   return model.UFO_UGCODE;
            else
                return null;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.USER_FUNCOPERATE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [USER_FUNCOPERATE] set ");
            strSql.Append("[UFO_FUNCCODE]=@UFO_FUNCCODE,");
            strSql.Append("[UFO_OPERATECODE]=@UFO_OPERATECODE,");
            strSql.Append("[UFO_HAVEFUNC]=@UFO_HAVEFUNC,");
            strSql.Append("[UFO_HAVEOPERATE]=@UFO_HAVEOPERATE,");
            strSql.Append("[UFO_DELETE]=@UFO_DELETE");
            strSql.Append(" where [UFO_UGCODE]=@UFO_UGCODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@UFO_UGCODE", SqlDbType.VarChar,20),
                    new SqlParameter("@UFO_FUNCCODE", SqlDbType.VarChar,15),
                    new SqlParameter("@UFO_OPERATECODE", SqlDbType.VarChar,15),
                    new SqlParameter("@UFO_HAVEFUNC", SqlDbType.SmallInt),
                    new SqlParameter("@UFO_HAVEOPERATE", SqlDbType.SmallInt),
                    new SqlParameter("@UFO_DELETE", SqlDbType.SmallInt)
            };
            parameters[0].Value = model.UFO_UGCODE;
            parameters[1].Value = model.UFO_FUNCCODE;
            parameters[2].Value = model.UFO_OPERATECODE;
            parameters[3].Value = model.UFO_HAVEFUNC;
            parameters[4].Value = model.UFO_HAVEOPERATE;
            parameters[5].Value = model.UFO_DELETE;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string UFO_UGCODE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete [USER_FUNCOPERATE] ");
            strSql.Append(" where [UFO_UGCODE]=@UFO_UGCODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@UFO_UGCODE", SqlDbType.VarChar,20)};
            parameters[0].Value = UFO_UGCODE;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string UFO_UGCODE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [USER_FUNCOPERATE]");
            strSql.Append(" where [UFO_UGCODE]= @UFO_UGCODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@UFO_UGCODE", SqlDbType.VarChar,20)};
            parameters[0].Value = UFO_UGCODE;
            return SqlHelper.Exists(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.USER_FUNCOPERATE GetModel(string UFO_UGCODE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [USER_FUNCOPERATE] ");
            strSql.Append(" where [UFO_UGCODE]=@UFO_UGCODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@UFO_UGCODE", SqlDbType.VarChar,20)};
            parameters[0].Value = UFO_UGCODE;
            DataSet ds = SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取数据集,建议只在多表联查时使用
        /// </summary>
        public DataSet GetDataSet(string strSql)
        {
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql);
        }

        /// <summary>
        /// 获取泛型数据列表,建议只在多表联查时使用
        /// </summary>
        public IList<Model.USER_FUNCOPERATE> GetList(string strSql)
        {
            return GetList(GetDataSet(strSql));
        }

        /// <summary>
        /// 获取数据集,在单表查询时使用
        /// </summary>
        public DataSet GetDataSet(string strWhat, string strWhere, string strOrderby)
        {
            if (string.IsNullOrEmpty(strWhat))
                strWhat = "*";
            StringBuilder strSql = new StringBuilder("select " + strWhat + " from [USER_FUNCOPERATE]");
            if (!string.IsNullOrEmpty(strWhere))
                strSql.Append(" where " + strWhere);
            if (!string.IsNullOrEmpty(strOrderby))
                strSql.Append(" order by " + strOrderby);
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString());
        }

        /// <summary>
        /// 获取泛型数据列表,在单表查询时使用
        /// </summary>
        public IList<Model.USER_FUNCOPERATE> GetList(string strWhat, string strWhere, string strOrderby)
        {
            return GetList(GetDataSet(strWhat, strWhere, strOrderby));
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.USER_FUNCOPERATE> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[USER_FUNCOPERATE]", null, pageSize, pageIndex, fldSort, sort, strCondition, "UFO_UGCODE", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.USER_FUNCOPERATE GetModel(DataRow r)
        {
            Model.USER_FUNCOPERATE model = new Model.USER_FUNCOPERATE();
            model.UFO_UGCODE = SqlHelper.GetString(r["UFO_UGCODE"]);
            model.UFO_FUNCCODE = SqlHelper.GetString(r["UFO_FUNCCODE"]);
            model.UFO_OPERATECODE = SqlHelper.GetString(r["UFO_OPERATECODE"]);
            model.UFO_HAVEFUNC = SqlHelper.GetInt(r["UFO_HAVEFUNC"]);
            model.UFO_HAVEOPERATE = SqlHelper.GetInt(r["UFO_HAVEOPERATE"]);
            model.UFO_DELETE = SqlHelper.GetInt(r["UFO_DELETE"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.USER_FUNCOPERATE> GetList(DataSet ds)
        {
            List<Model.USER_FUNCOPERATE> l = new List<Model.USER_FUNCOPERATE>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
